<template>
  <div class="register">
    <!-- 注册表单 -->
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="rules"
      class="register_form"
    >
      <h3 class="title">敢心商城</h3>
      <el-form-item prop="nickName">
        <el-input
          type="text"
          v-model="registerForm.nickName"
          placeholder="昵称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="userName">
        <el-input
          type="text"
          v-model="registerForm.userName"
          placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="registerForm.password"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          type="password"
          v-model="registerForm.checkPass"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="smsCode">
        <el-input
          v-model="registerForm.smsCode"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="submitForm"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <el-button round @click.native.prevent="getSmsCode">{{
            computeTime > 0 ? `(${computeTime}s)已发送` : "获取验证码"
          }}</el-button>
        </div>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="请输入验证码"
          style="width: 63%"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-form-item class="btn">
        <el-button
          style="float: left; width: 45%"
          type="primary"
          @click.native.prevent="submitForm"
        >
          <span>注 册</span>
        </el-button>
        <el-button
          style="float: right; width: 45%"
          @click="resetForm('registerForm')"
          >重置</el-button
        >
      </el-form-item>
      <router-link to="/login">
        <el-form-item style="width: 100%">
          <el-button size="medium" type="text" style="width: 100%">
            <span>已有账号？点我登录</span>
          </el-button>
        </el-form-item>
      </router-link>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2021 cqjtu pjy&zq.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, getSmsCode, register } from "@/api/login";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  name: "Register",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      codeUrl: "",
      computeTime: 0,
      registerForm: {
        nickName: "",
        userName: "",
        password: "",
        checkPass: "",
        code: "",
        uuid: "",
        smsCode: "",
        cookiePassword: "",
        computeTime: 0,
      },
      rules: {
        nickName: [
          { required: true, trigger: "blur", message: "昵称不能为空" },
          { min: 1, max: 12, message: "昵称不能超过12位", trigger: "blur" },
        ],
        userName: [
          { required: true, trigger: "blur", message: "手机号不能为空" },
          { validator: checkPhone, trigger: "blur" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
          { min: 1, max: 8, message: "密码长度不能超过8位", trigger: "blur" }
        ],
        checkPass: [
          { required: true, trigger: "blur", message: "密码不能为空" },
          { validator: validatePass, trigger: "blur" },
        ],
        code: [{ required: true, trigger: "blur",message:"请输入验证码" }],
        smsCode: [{ required: true, trigger: "blur",message:"请输入短信验证码" }],
      },
      loading: false,
      redirect: undefined,
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getSmsCode() {
      if (!this.computeTime) {
        this.$refs.registerForm.validateField("userName", error => {
          if (!error) {
            getSmsCode(this.registerForm.userName).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: "验证码已发送",
                  type: "success",
                });
                this.registerForm.uuid = res.uuid;
                this.computeTime = 60;
                this.timer = setInterval(() => {
                  this.computeTime--;
                  if (this.computeTime <= 0) {
                    clearInterval(this.timer);
                  }
                }, 1000);
              }
            });
          }
        });
      }
    },
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
      });
    },
    submitForm() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          console.log("验证成功，调用register方法");
          console.log(this.registerForm);
          register(this.registerForm).then((res) => {
            console.log("得到返回结果");
            if (res.code === 200) {
              this.$message({
                message: "注册成功",
                type: "success",
              });
              this.$router.push({ path: "/login", replace: true });
            }
          });
        }
      });

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register_form {
  border-radius: 12px;
  width: 400px;
  box-shadow: 0 5px 20px 0 rgba(0, 0, 0, 0.192);
  background-color: rgba(255, 255, 255, 0.904);
  box-sizing: border-box;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 35px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.btn {
  padding: 5px;
}
.login-code-img {
  height: 38px;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
